package inc.sneid.reminderSpring.controllers;

import inc.sneid.reminderSpring.domains.Deltas;
import inc.sneid.reminderSpring.domains.Record;
import inc.sneid.reminderSpring.domains.RecordCreationDto;
import inc.sneid.reminderSpring.exceptions.DeltasNotFoundException;
import inc.sneid.reminderSpring.services.DeltasService;
import inc.sneid.reminderSpring.services.RecordService;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("record")
public class RecordController {
    private final RecordService recordService;
    private final DeltasService deltasService;

    public RecordController(RecordService recordService, DeltasService deltasService) {
        this.recordService = recordService;
        this.deltasService = deltasService;
    }

    @PostMapping("new")
    public Record newRecord(@RequestBody @Valid RecordCreationDto recordDto) throws DeltasNotFoundException {
        Deltas deltas = deltasService.getByName(recordDto.getDeltasName());
        Record record = new Record(recordDto.getName(), recordDto.getDescription(), deltas);
        return recordService.save(record);
    }

    @GetMapping("{id}")
    public Record get(@PathVariable(name = "id") Record record) {
        return record;
    }

    @GetMapping("all")
    public Page<Record> getAll(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size){
        Pageable pageable = PageRequest.of(page, size);
        return recordService.getPage(pageable);
    }

    @DeleteMapping
    public void delete(@RequestParam Long id){
        recordService.deleteById(id);
    }
}
